package com.tanhua.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.model.domain.Analysis;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


public interface AnalysisMapper extends BaseMapper<Analysis> {
    /**
     * 查询某天日志文件的汇总
     */
    @Select("SELECT * FROM tb_analysis_by_day WHERE record_date = #{now}")
    Analysis queryAnalysisByDay(@Param("now") String now);

    /**
     * 查询总用户数
     * @return
     */
    @Select("select sum(num_registered) from tb_analysis_by_day")
    Integer queryCumulativeUsers();

    /**
     * 查询30天/7天活跃用户数
     */
    @Select("SELECT sum( num_active ) FROM tb_analysis_by_day WHERE date_sub( #{now}, INTERVAL #{day} DAY ) <= date(record_date)")
    Integer queryActiveSum_day(@Param("now") String now, @Param("day") int day);

    /**
     * 查询今日活跃用户数
     */
    @Select("SELECT num_active FROM tb_analysis_by_day WHERE to_days( record_date ) = to_days( #{now} )")
    Integer queryActiveSum_today(@Param("now") String now);

    /**
     * 查询今日登录用户
     */
    @Select("SELECT num_login FROM tb_analysis_by_day WHERE to_days( record_date ) = to_days( #{now} )")
    Integer queryNumLogin_today(@Param("now") String now);

    /**
     * 查询时间范围内的-注册用户数
     * @param sd
     * @param ed
     * @return
     */
    @Select("SELECT record_date,num_registered FROM tb_analysis_by_day WHERE record_date BETWEEN #{sd} AND #{ed} ORDER BY record_date")
    List<Analysis> queryRegister_days(@Param("sd") String sd, @Param("ed") String ed);

    /**
     * 查询时间范围内的-活跃用户数
     * @param sd
     * @param ed
     * @return
     */
    @Select("SELECT record_date,num_active FROM tb_analysis_by_day WHERE record_date BETWEEN #{sd} AND #{ed} ORDER BY record_date")
    List<Analysis> queryActive_days(@Param("sd") String sd, @Param("ed") String ed);

    /**
     * 查询次日留存用户数据
     * @param sd
     * @param ed
     * @return
     */
    @Select("SELECT record_date,num_registered,num_retention1d FROM tb_analysis_by_day WHERE record_date BETWEEN DATE_SUB(#{sd},INTERVAL 1 DAY) AND #{ed} ORDER BY record_date")
    List<Analysis> queryRetention1d(@Param("sd") String sd, @Param("ed") String ed);
}
